<?php

$db = new WebSalesDB();
$badchars = array('<', '>', '€', '&');
$goodchars = array('&lt;', '&gt;', '&#8364;', '&amp;');

function array_to_xml($array) {
    $xml = '';
    foreach ($array as $key => $value) {
        $key = ucwords(strtolower($key));
        $xml .= "\t\t\t\t\t<$key>$value</$key>\n";
    }
    return $xml;
}

function CreateIndividualXML($Product) {
    global $badchars;
    global $goodchars;
    if ($Product['WebRPMED'] == 0) {
        $percentage = 0;
    } else {
        $percentage = (($Product['WebRPMED'] - $Product['WebRPHIGH']) / $Product['WebRPMED']) * 100;
    }
    if ($percentage > 0) {
        $percentage = "Save " . number_format($percentage) . "&#37;";
    } else {
        $percentage = "";
    }
    $prod_fp = fopen('../xml/individuals/' . $Product['StockCode'] . '.xml', 'w');
    fwrite($prod_fp, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
    fwrite($prod_fp, "<ProductItem>\n");
    fwrite($prod_fp, "\t<Name>" . str_replace($badchars, $goodchars, $Product['WebDesc']) . "</Name>\n");
    fwrite($prod_fp, "\t<Brand>" . str_replace($badchars, $goodchars, $Product['Manufacturer']) . "</Brand>\n");
    fwrite($prod_fp, "\t<Stockcode>" . str_replace($badchars, $goodchars, $Product['StockCode']) . "</Stockcode>\n");
    fwrite($prod_fp, "\t<tpl>" . str_replace($badchars, $goodchars, $Product['MasterCat']) . "</tpl>\n");
    fwrite($prod_fp, "\t<mcl>" . str_replace($badchars, $goodchars, $Product['WebCat']) . "</mcl>\n");
    fwrite($prod_fp, "\t<scl>" . str_replace($badchars, $goodchars, $Product['SubCat']) . "</scl>\n");
    fwrite($prod_fp, "\t<ShortDescription>" . str_replace($badchars, $goodchars, $Product['WebDesc']) . "</ShortDescription>\n");
    fwrite($prod_fp, "\t<RRP>" . str_replace($badchars, $goodchars, $Product['WebRPMED']) . "</RRP>\n");
    fwrite($prod_fp, "\t<OurPrice>" . str_replace($badchars, $goodchars, $Product['WebRPHIGH']) . "</OurPrice>\n");
    fwrite($prod_fp, "\t<Saving>" . str_replace($badchars, $goodchars, $percentage) . "</Saving>\n");
    fwrite($prod_fp, "\t<Features>" . str_replace($badchars, $goodchars, $Product['ExtendedDesc']) . "</Features>\n");
    fwrite($prod_fp, "\t<Specifications>" . str_replace($badchars, $goodchars, $Product['ExtendedDesc']) . "</Specifications>\n");
    fwrite($prod_fp, "\t<ProductImages>\n");
    fwrite($prod_fp, "\t\t<Image1></Image1>\n");
    fwrite($prod_fp, "\t</ProductImages>\n");
    fwrite($prod_fp, "\t<ProductReviews>\n");
    fwrite($prod_fp, "\t\t<Review1></Review1>\n");
    fwrite($prod_fp, "\t</ProductReviews>\n");
    fwrite($prod_fp, "</ProductItem>\n");
    fclose($prod_fp);
    echo '../xml/individuals/' . $Product['StockCode'] . '.xml<br />';
}

$fp = fopen('../xml/products.xml', 'w');
fwrite($fp, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<Categories>\n");

$TopLevelCategory = "SELECT DISTINCT MasterCat,MasterCatID FROM CostBiterStock WHERE MasterCat != ''";
$TLC_qry = DB::query($TopLevelCategory);

while ($TLC = DB::fetch($TLC_qry)) {
    fwrite($fp, "\t<Category Name='" . $TLC['MasterCat'] . "'>\n");
    $MasterCategory = "SELECT DISTINCT WebCatID,WebCat FROM CostBiterStock WHERE MasterCatID = '$TLC[MasterCatID]'";
    $MasterCat_qry = DB::query($MasterCategory);

    while ($MasterCat = DB::fetch($MasterCat_qry)) {
        fwrite($fp, "\t\t<Master Name='" . str_replace($badchars, $goodchars, $MasterCat['WebCat']) . "'>\n");
        $SubCategory = "SELECT DISTINCT SubCatID,SubCat FROM CostBiterStock WHERE MasterCatID = '$TLC[MasterCatID]' AND WebCatID = '$MasterCat[WebCatID]'";
        $SubCat_qry = DB::query($SubCategory);

        while ($SubCat = DB::fetch($SubCat_qry)) {
            fwrite($fp, "\t\t\t<Sub Name='" . str_replace($badchars, $goodchars, $SubCat['SubCat']) . "'>\n");
            $Products = "SELECT * FROM CostBiterStock " .
                    "WHERE MasterCatID = '$TLC[MasterCatID]' AND WebCatID = '$MasterCat[WebCatID]' AND SubCatID = '$SubCat[SubCatID]'";
            $Prd_qry = DB::query($Products);

            while ($Gifts = DB::fetch($Prd_qry)) {
                if ($Gifts['WebRPMED'] == 0) {
                    $percentage = 0;
                } else {
                    $percentage = (($Gifts['WebRPMED'] - $Gifts['WebRPHIGH']) / $Gifts['WebRPMED']) * 100;
                }
                fwrite($fp, "\t\t\t\t<Productitem Name='" . str_replace($badchars, $goodchars, $Gifts['WebDesc']) . "'>\n");
                $WebSafe_Link = "http://dev.costbiter.co.uk/gift/" . str_replace(" ", "-", str_replace($badchars, $goodchars,  strtolower($Gifts['WebDesc']))) . "/" . $Gifts['StockCode'] . ".html";
                $ImageURL = "http://dev.costbiter.co.uk/web3g/img/accessories/" . $Gifts['StockCode'] . ".jpg";
                $Promotions =
                        array(
                            'Name' => str_replace($badchars, $goodchars,  $Gifts['WebDesc']),
                            'Brand' => str_replace($badchars, $goodchars, $Gifts['Manufacturer']),
                            'StockCode' => str_replace($badchars, $goodchars, $Gifts['StockCode']),
                            'Description' => str_replace($badchars, $goodchars, $Gifts['ExtendedDesc']),
                            'RRP' => str_replace($badchars, $goodchars, $Gifts['WebRPMED']),
                            'Ourprice' => str_replace($badchars, $goodchars, $Gifts['WebRPHIGH']),
                            'Saving' => number_format($percentage) . "&#37;",
                            'Image' => $ImageURL,
                            'Link' => $WebSafe_Link
                );
                fwrite($fp, array_to_xml($Promotions));
                fwrite($fp, "\t\t\t\t</Productitem>\n");
                CreateIndividualXML($Gifts);
            }
            fwrite($fp, "\t\t\t</Sub>\n");
        }
        fwrite($fp, "\t\t</Master>\n");
    }
    fwrite($fp, "\t</Category>\n");
}
fwrite($fp, "</Categories>\n");
fclose($fp);
echo "File 'products.xml' created";
?>